package com.mndp.system.service.impl;

import com.mndp.common.exception.MyException;
import com.mndp.system.domain.vo.TestPeopleAddVo;
import com.mndp.system.domain.vo.TestPeoplePageVo;
import com.mndp.system.domain.vo.TestPeopleUpdVo;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mndp.system.domain.po.TestPeople;
import com.mndp.system.mapper.TestPeopleMapper;
import com.mndp.system.service.TestPeopleService;

import java.util.List;

@Service
public class TestPeopleServiceImpl extends ServiceImpl<TestPeopleMapper, TestPeople> implements TestPeopleService {

    @Resource
    private TestPeopleMapper testPeopleMapper;

    private LambdaQueryWrapper<TestPeople> getWrapper(TestPeoplePageVo vo) {
        LambdaQueryWrapper<TestPeople> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ObjectUtil.isNotEmpty(vo.getName()), TestPeople::getName, vo.getName());
        wrapper.eq(ObjectUtil.isNotEmpty(vo.getAge()), TestPeople::getAge, vo.getAge());
        wrapper.eq(ObjectUtil.isNotEmpty(vo.getSex()), TestPeople::getSex, vo.getSex());
        String startTime = vo.getStartTime();
        String endTime = vo.getEndTime();
        wrapper.between(ObjectUtil.isNotEmpty(startTime) && ObjectUtil.isNotEmpty(endTime), TestPeople::getCreateTime, startTime, endTime);
        return wrapper;
    }

    @Override
    public IPage<TestPeople> getPage1(TestPeoplePageVo vo) {
        QueryWrapper<TestPeople> wrapper = new QueryWrapper<>();
        wrapper.eq(ObjectUtil.isNotEmpty(vo.getName()), "tp.name", vo.getName());
        wrapper.eq(ObjectUtil.isNotEmpty(vo.getAge()), "tp.age", vo.getAge());
        wrapper.eq(ObjectUtil.isNotEmpty(vo.getSex()), "tp.sex", vo.getSex());
//        wrapper.eq("tp.isDelete", 0);
        String startTime = vo.getStartTime();
        String endTime = vo.getEndTime();
        wrapper.between(ObjectUtil.isNotEmpty(startTime) && ObjectUtil.isNotEmpty(endTime), "tp.create_time", startTime, endTime);
        return testPeopleMapper.getPageWrapper(vo.getPage(true), wrapper);
    }

    @Override
    public IPage<TestPeople> getPage2(TestPeoplePageVo vo) {
        Page<TestPeople> page = new Page<>(vo.getPageNum(), vo.getPageSize());
        return testPeopleMapper.getPage(page, vo);
    }

    @Override
    public void addInfo(TestPeopleAddVo vo) throws MyException {
        TestPeople testPeople = new TestPeople();
        testPeople.setName(vo.getName());
        testPeople.setAge(vo.getAge());
        testPeople.setSex(vo.getSex());
        testPeopleMapper.insert(testPeople);
    }

    @Override
    public TestPeople getInfo(String id) {
        return testPeopleMapper.selectById(id);
    }

    @Override
    public void editInfo(TestPeopleUpdVo vo) throws MyException {
        LambdaUpdateWrapper<TestPeople> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(TestPeople::getId, vo.getId());
        updateWrapper.set(TestPeople::getName, vo.getName());
        updateWrapper.set(TestPeople::getAge, vo.getAge());
        updateWrapper.set(TestPeople::getSex, vo.getSex());
        testPeopleMapper.update(new TestPeople(), updateWrapper);
    }

    @Override
    public void deleteByIds(List<String> ids) throws MyException {
        testPeopleMapper.deleteBatchIds(ids);
    }
}
